package cz.via.facepalm.calendar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import cz.via.facepalm.shared.CActivity;
import cz.via.facepalm.shared.CUser;
import cz.via.facepalm.shared.CUserActivity;

public class OthersAttendanceActivity extends Activity 
{
	CUser user;
	CActivity activity;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.others_attendence);
		
		Bundle data = getIntent().getExtras();
		CUser u = (CUser)(data.getSerializable("user"));
		if(u==null){
			throw new RuntimeException("Hacking attempt exception");
		}
		this.user = u;
		this.activity = (CActivity)(data.getSerializable("cactivity"));
		
		
	    Button btn = (Button) findViewById(R.id.context_btn);  
	    registerForContextMenu(btn);
	    	         
	    ServerComunicator.listUsersForActivity(activity.getId(), new DoneCallback<List<CUserActivity>>() {			
			@Override
			public void done(List<CUserActivity> list) 
			{
				final List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
			    for(CUserActivity cua : list)
			    {			    	
			    	
			    	HashMap<String, String> map = new HashMap<String, String>();
			    	map.put("username", cua.getIdUser().getName());
			    	map.put("percents", cua.getPercentComming()+"% ");
			    	map.put("time", ActivityHelper.getTimeWithPct(cua.getIdActivity(), cua.getPercentLate()/100.0f));					    	
			    	fillMaps.add(map);
			    }	    
			    
			    OthersAttendanceActivity.this.runOnUiThread(new Runnable() {
			        public void run() {
			        	
			        	String[] from = new String[] {"username","percents","time"};
			    		int[] to = new int[] {R.id.username ,R.id.percents,R.id.time};
			    		
			    		ListView othersList = (ListView)findViewById(R.id.others_list);
			    		othersList.setItemsCanFocus(true);
			    		final SimpleAdapter source = new SimpleAdapter(OthersAttendanceActivity.this, fillMaps,
			    				R.layout.others_attendence_row,from, to);			    		
			    		othersList.setAdapter(source);			    				   
			    		
			    		source.notifyDataSetChanged();
			    	
			        }
			    });
				
			}
			
		});	    	   
	    
	    Button backBtn = (Button) findViewById(R.id.back_btn);
		backBtn.setOnClickListener(new View.OnClickListener() 
		{
			public void onClick(View v) 
			{	
				Intent intent = new Intent(OthersAttendanceActivity.this,
						ActivityDetail.class);
		    	Bundle credentials = new Bundle();							
				credentials.putSerializable("user", OthersAttendanceActivity.this.user);
				credentials.putLong("activityId", activity.getId());
				intent.putExtras(credentials);
				startActivity(intent);
			}
		});
	}		
	
	
	
	@Override  
    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) 
    {  
    	super.onCreateContextMenu(menu, v, menuInfo);  
	    menu.setHeaderTitle("Actions");  	     
	    menu.add(0, v.getId(), 0, "List activities");	    
    }
	
	@Override  
	public boolean onContextItemSelected(MenuItem item) 
	{  
	    if(item.getTitle()=="List activities"){
	    	
	    	Intent intent = new Intent(OthersAttendanceActivity.this,
					CalendarActivity.class);
	    	Bundle credentials = new Bundle();							
			credentials.putSerializable("user", this.user);
			intent.putExtras(credentials);
			startActivity(intent);
	    }  
	    else return false;
	    
	    return true;  
	}  
}